﻿package mortal.common.preLoadPage
{
    import com.gengine.debug.*;
    import com.gengine.utils.*;
    import flash.events.*;
    import flash.net.*;

    public class GametipsConfig extends Object
    {
        private var _intervalTime:int = 3;
        private var _tipsFirst:Array;
        private var _tips:Array;
        public var firstTipIndex:int = 0;
        private static var _instance:GametipsConfig;

        public function GametipsConfig()
        {
            if (_instance)
            {
                throw new Error("GametipsConfig 单例！");
            }
            return;
        }// end function

        public function get intervalTime() : int
        {
            return this._intervalTime;
        }// end function

        public function load(param1:URLRequest, param2:Function) : void
        {
            var onLoaderComplete:Function;
            var onLoaderError:Function;
            var urlReq:* = param1;
            var success:* = param2;
            onLoaderComplete = function (event:Event) : void
            {
                var _loc_3:XML = null;
                var _loc_4:int = 0;
                var _loc_5:String = null;
                var _loc_2:* = new XML((event.target as URLLoader).data);
                _intervalTime = parseInt(_loc_2.child("intervalTime").@value);
                _tipsFirst = new Array();
                _tips = new Array();
                for each (_loc_3 in _loc_2.child("item"))
                {
                    
                    _loc_4 = int(_loc_3.@type);
                    _loc_5 = HTMLUtil.getHtmlByComp(String(_loc_3.@value));
                    if (_loc_4 == 1)
                    {
                        _tipsFirst.push(_loc_5);
                        continue;
                    }
                    _tips.push(_loc_5);
                }
                success.call();
                return;
            }// end function
            ;
            onLoaderError = function (event:IOErrorEvent) : void
            {
                Log.system("Gametips.xml 加载失败！");
                return;
            }// end function
            ;
            var loader:* = new URLLoader();
            loader.addEventListener(Event.COMPLETE, onLoaderComplete);
            loader.addEventListener(IOErrorEvent.IO_ERROR, onLoaderError);
            loader.load(urlReq);
            return;
        }// end function

        public function getTip() : String
        {
            var _loc_1:String = null;
            if (this._tipsFirst)
            {
                if (this.firstTipIndex < this._tipsFirst.length)
                {
                    _loc_1 = this._tipsFirst[this.firstTipIndex] as String;
                    var _loc_2:String = this;
                    var _loc_3:* = this.firstTipIndex + 1;
                    _loc_2.firstTipIndex = _loc_3;
                    return _loc_1;
                }
            }
            return this.getRandomTip();
        }// end function

        public function getRandomTip() : String
        {
            var _loc_1:int = 0;
            if (this._tips && this._tips.length >= 1)
            {
                _loc_1 = Math.round((this._tips.length - 1) * Math.random());
                return this._tips[_loc_1] as String;
            }
            return "";
        }// end function

        public static function get instance() : GametipsConfig
        {
            if (!_instance)
            {
                _instance = new GametipsConfig;
            }
            return _instance;
        }// end function

    }
}
